#include "tank.h"
#include "ui_tank.h"
#include<QPainter>
#include<QTimer>
#include<QKeyEvent>
#include<QtGlobal>

tank::tank(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::tank),start(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(200,200,520,520));
}

tank::~tank()
{
    delete ui;
}
int map[26][26] = {  { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,2,2,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,2,2,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
                     { 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1 },
                     { 2,2,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,2,2 },
                     { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,0,1,4,4,1,0,0,0,0,0,0,0,0,0,0,0 },
                     { 0,0,0,0,0,0,0,0,0,0,0,1,4,4,1,0,0,0,0,0,0,0,0,0,0,0 } };

void tank::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if(!start)
    Inittank();
    //painter.setPen(Qt::blue);
   /* for(int i=0;i<=26;i++)
    {
        painter.drawLine(0,i*20,520,i*20);//横
        painter.drawLine(i*20,0,i*20,520);
    }*/
    for(int i=0;i<=26;i++)
    {
        for(int j=0;j<=26;j++)
        {
            if(map[i][j]==0)//空地
            {
                 //painter.setPen(Qt::black);
                 painter.setBrush(Qt::black);
                 painter.drawRect(j*20,i*20,20,20);
            }
            if(map[i][j]==1)//土块
            {
                //painter.setPen(Qt::green);
                //painter.setBrush(Qt::red);
                painter.drawRect(j*20,i*20,20,20);
                painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/wall1.jpg"));
            }
            if(map[i][j]==2)//铁块
            {
                //painter.setPen(Qt::green);
                painter.setBrush(Qt::green);
                painter.drawRect(j*20,i*20,20,20);
                painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/wall2.jpg"));
            }
            if(map[i][j]==4)//基地
            {
                //painter.setBrush(Qt::black);
                painter.drawRect(j*20,i*20,20,20);
                painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/home.jpg"));
            }
            if(map[i][j]==9)//子弹
            {
                //painter.setBrush(Qt::blue);
                painter.drawRect(j*20,i*20,20,20);
                painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/bullet.jpeg"));
            }
            if(map[i][j]==5)//mytank
            {
                painter.setBrush(Qt::yellow);
                painter.drawRect(j*20,i*20,20,20);
                if(ndirection==1)painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/tank_up.jpg"));
                if(ndirection==2)painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/tank_down.jpg"));
                if(ndirection==3)painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/tank_left.jpg"));
                if(ndirection==4)painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/images/images/tank_right.jpg"));
            }
        }
    }
}
void tank::Inittank()
{
    start=true;
   mytank.x=24;mytank.y=9;
   map[mytank.x][mytank.y]=5;
   ndirection=1;
   mytanktimer=new QTimer(this);
   mytanktimer->start(400);
   connect(mytanktimer,SIGNAL(timeout()),SLOT(tank_update()));


   mybullettimer=new QTimer(this);
   mybullettimer->start(121);
   connect(mybullettimer,SIGNAL(timeout()),SLOT(bullet_update()));


}
void tank::bullet_update()
{
    if(shoot==true)
    {
        for(int i=0;i<100;i++)
        {
            if(bullet[i].life==false)
            {
                bullet[i].direction=ndirection;
                bullet[i].x=mytank.x; bullet[i].y=mytank.y;
                bullet[i].life=true;
                break;
            }
        }
        shoot=false;
    }
    for(int i=0;i<100;i++)
    {
        if(bullet[i].life==true)
        {
            map[bullet[i].x][bullet[i].y]=0;
            if(bullet[i].direction==1)bullet[i].x--;
            if(bullet[i].direction==2)bullet[i].x++;
            if(bullet[i].direction==3)bullet[i].y--;
            if(bullet[i].direction==4)bullet[i].y++;
            if(map[bullet[i].x][bullet[i].y]==4)
            {
                mytanktimer->stop();
                mybullettimer->stop();
            }
            if(map[bullet[i].x][bullet[i].y]==2)
            {
                bullet[i].life=false;
                break;
            }
            if(bullet[i].x<=0||bullet[i].x>=520||bullet[i].y<=0||bullet[i].y>=520||map[bullet[i].x][bullet[i].y]==1)
            {
                bullet[i].life=false;
                map[bullet[i].x][bullet[i].y]=0;
            }
            else map[bullet[i].x][bullet[i].y]=9;
            /*if(bullet[i].y<0||bullet[i].y>520)bullet[i].life=false;
            if(map[bullet[i].x][bullet[i].y]==1)
            {
                bullet[i].life=false;
                map[bullet[i].x][bullet[i].y]=0;
            }*/
            //if(map[bullet[i].x][bullet[i].y]==1)bullet[i].life=false;
        }
    }
    update();
}
void tank::tank_update()
{
    map[mytank.x][mytank.y]=0;
    int x=mytank.x,y=mytank.y;
    switch(ndirection)
     {
     case 1:
    {
        if(map[x-1][y]!=1&&map[x-1][y]!=2)mytank.x--;
         break;
    }
     case 2:
    {
        if(map[x+1][y]!=1&&map[x+1][y]!=2)mytank.x++;
         break;
    }
     case 3:
    {
        if(map[x][y-1]!=1&&map[x][y-1]!=2)mytank.y--;
         break;
    }
     case 4:
    {
        if(map[x][y+1]!=1&&map[x][y+1]!=2)mytank.y++;
         break;
    }
     default:;
     }
    map[mytank.x][mytank.y]=5;
    update();
}


void tank::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*) event;
    switch(key->key())
    {
    case Qt::Key_W:ndirection=1;
       break;
    case Qt::Key_S:ndirection=2;
     break;
    case Qt::Key_A:ndirection=3;
     break;
    case Qt::Key_D:ndirection=4;
     break;
    case Qt::Key_J:shoot=true;
       break;
     default:;
    }
}
